//
//  EditPermissionUserController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "EditPermissionUserController.h"

#import "FormattingUtilities.h"

@implementation EditPermissionUserController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.dataSourceDictionary setObject:@"" forKey:@"user"];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	[self.nameCtl becomeFirstResponder];
}

- (void)save:(id)sender {
	NSString *nameStr = [[nameCtl text] stringByTrimmingCharactersInSet:
                         [NSCharacterSet whitespaceCharacterSet]];
    
    if ([nameStr length]) {
        [self.dataSourceDictionary setObject:nameStr forKey:@"user"];
        [super save:self];
    } else {
        [self done:sender];
    }
}

- (void)done:(id)sender {
	[self.nameCtl resignFirstResponder];
	[super done:self];
}

//MARK: -
//MARK: Controls and functions

- (id)nameCtl {
	
	if (nameCtl == nil) {
		nameCtl = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 10.0, 290.0, 24.0)];
		((UITextField *)nameCtl).borderStyle = UITextBorderStyleNone;
		((UITextField *)nameCtl).adjustsFontSizeToFitWidth = YES;
		((UITextField *)nameCtl).minimumFontSize = 9.0;
		((UITextField *)nameCtl).textColor = [UIColor colorWithRed:0.32 green:0.4 blue:0.57 alpha:1];
		((UITextField *)nameCtl).backgroundColor = [UIColor clearColor];
		((UITextField *)nameCtl).autocorrectionType = UITextAutocorrectionTypeNo;
		((UITextField *)nameCtl).keyboardType = UIKeyboardTypeDefault;
		((UITextField *)nameCtl).returnKeyType = UIReturnKeyDone;
		((UITextField *)nameCtl).clearButtonMode = UITextFieldViewModeWhileEditing;
		((UITextField *)nameCtl).autoresizingMask = UIViewAutoresizingFlexibleWidth;
		((UITextField *)nameCtl).delegate = self;
		((UITextField *)nameCtl).placeholder = LocalizedString(@"User");
	}
	
	((UITextField *)nameCtl).text = [self.dataSourceDictionary objectForKey:@"user"];
	
	return nameCtl;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	[self save:self];
	return YES;
}

@end

